package com.depin.pdaapp.utils;

import android.net.ParseException;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by lzn.
 * Date：2020/2/21 13:47
 * Desc：
 **/
public class TimeUtil {
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat formatHHDDSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat HHDD = new SimpleDateFormat("HH:mm");
    public static SimpleDateFormat HHDDSS = new SimpleDateFormat("HH:mm:ss");
    public static SimpleDateFormat formatHH = new SimpleDateFormat("yyyyMMddHH:mm");
    public static SimpleDateFormat formatDD = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat yyyyMMddHHss = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
    public static final long ONE_DAY_MS = 24 * 60 * 60 * 1000;
    /**
     *
     * @param time  1541569323155
     * @return 2018-11-07 13:42:03
     */
    public static String getDate2String(long time) {
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return format.format(date);
    }
    public static String getNowDate() {

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return format.format(nowDate);
    }


    public static String getNowDateTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return yyyyMMddHHss.format(nowDate);
    }


    public static String getNowTimes() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return HHmm.format(nowDate);
//        return "02:00";
    }

    public static String getNowStringTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return HHDD.format(nowDate);
    }

    public static String getHHmmss(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return formatHHDDSS.format(nowDate);
    }

    public static long getStringToDate(String dateString, SimpleDateFormat dateFormat) {
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return ts;
    }

    /**
     * 计算两个日期之间的日期
     *
     * @param startTime
     * @param endTime
     */
    public static List<String> betweenDays(String startTime, String endTime) {
        List<String> datalist = new ArrayList<>();
        Date date_start = new Date(dateToStamp(startTime));
        Date date_end = new Date(dateToStamp(endTime));
        //计算日期从开始时间于结束时间的0时计算
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(date_start);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(date_end);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        int s = (int) ((toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis()) / (ONE_DAY_MS));
        if (s > 0) {
            for (int i = 0; i <= s; i++) {
                long todayDate = fromCalendar.getTimeInMillis() + i * ONE_DAY_MS;
                /**
                 * yyyy-MM-dd E :2012-09-01
                 */
                Log.i("打印日期", getCustonFormatTime(todayDate, "yyyy-MM-dd"));
                datalist.add(getCustonFormatTime(todayDate, "yyyy-MM-dd"));
                //取出多少号  添加集合  九宫格展示
            }
        } else {//此时在同一天之内
            Log.i("打印日期", getCustonFormatTime(dateToStamp(startTime), "yyyy-MM-dd"));
            datalist.add(getCustonFormatTime(dateToStamp(startTime), "yyyy-MM-dd"));
        }
        return datalist;
    }

    /**
     * 格式化传入的时间
     *
     * @param time      需要格式化的时间
     * @param formatStr 格式化的格式
     * @return
     */
    public static String getCustonFormatTime(long time, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date d1 = new Date(time);
        return format.format(d1);
    }

    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    /**
     * 获取前n天日期、后n天日期
     *
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldFormateDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    /**
     * @param distanceDay 前几天 如获取前7天日期则传-7即可；如果后7天则传7
     * @return
     */
    public static String getOldDate(String distanceDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = format.parse(distanceDay);

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 6);
        String dayBefore = format.format(c.getTime());
        return dayBefore;
    }

    public static String getLastDay(String nowDate) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = format.parse(nowDate);

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        String dayBefore = format.format(c.getTime());
        return dayBefore;
    }

    //计算某个时间在某个时间段的占比
    public static float getTimeScale(String nowDate, String starttime, String endtime) {
        List<String> dates = betweenDays(starttime, endtime);
        float x = ((getStringToDate(nowDate, TimeUtil.formatHHDDSS) - getStringToDate(starttime, format))) / (86400000f * dates.size());
        return x * dates.size();
    }

    //计算某个时间在某个时间段的占比
    public static float getDayTimeScale(String nowDate, String starttime) {
        float x = ((getStringToDate(nowDate, TimeUtil.formatHHDDSS) - getStringToDate(starttime, TimeUtil.format))) / 86400000f;
        return x * 6;
    }

    public static String getNextDay(String nowDate) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = format.parse(nowDate);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);

        String dayAfter = format.format(c.getTime());
        return dayAfter;
    }


    public static String getPickDate(String pickDate) {
        Date date = null;
        try {
            date = format.parse(pickDate);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return format.format(date);
    }

    public static String getPickTime(String pickTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(pickTime);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return simpleDateFormat.format(date);
    }



    public static Date getNowTime() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        Date nowDate = c.getTime();
        return nowDate;
    }


    public static String dateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = simpleDateFormat.format(date);
        return time;
    }

    /**
     * 字符串日期格式的计算
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        long between_days = 0;
        try {
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static String getDateAndTime(String pick_time) {
        Date date = null;
        try {
            date = yyyyMMddHHss.parse(pick_time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return yyyyMMddHHss.format(date);
    }
    public static String getClockTime(String pick_time) {
        Date date = null;
        try {
            date = formatHHDDSS.parse(pick_time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return HHDD.format(date);
    }

    public static String getTime(String pick_time) {
        Date date = null;
        try {
            date = HHDD.parse(pick_time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return HHDD.format(date);
    }

    public static int getTimeCompareSize(String startTime, String endTime, SimpleDateFormat format) {
        int i = 0;
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//年-月-日
        try {
            Date date1 = format.parse(startTime);//开始时间
            Date date2 = format.parse(endTime);//结束时间
            // 1 开始时间大于结束时间 2 开始时间与结束时间相同 3 开始时间小于结束时间
            if (date2.getTime() < date1.getTime()) {
                i = 1;
            } else if (date2.getTime() == date1.getTime()) {
                i = 2;
            } else if (date2.getTime() > date1.getTime()) {
                //正常情况下的逻辑操作.
                i = 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return i;
    }


    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }
}
